package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class BreadCrumbsItemControl extends Sprite
	{
		public static var EVENT_CLICK:String = "HELPBUTTON_EVENT_CLICK";
		public static var STYLE_ROOT:uint = 0;
		public static var STYLE_LIST:uint = 1;
		private static const TXT_CHILD_NAME:String = "txt";
		private static const FONT_SIZE:uint = AppGlobal.FONT_SMALL_SIZE;
		private static const FONT_COLOR:uint = 0xE381B2;
		private static const TEXT_HEIGHT:uint = 27;
		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var icon:Sprite;
		private var rounded:uint = 20;
		
		private var main:Sprite = new Sprite();
		private var base:Sprite = new Sprite();
		private var over:Sprite = new Sprite();
		private var press:Sprite = new Sprite();
		private var panel:Sprite;
		
		public var onClick:Signal = new Signal(Object);


		public function BreadCrumbsItemControl(data:Object, vWidth:uint, vHeight:uint)
		{
			super();
			this.data = data;
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			addChild(main);
			main.addChild(base);
			main.addChild(over);
			main.addChild(press);
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			display();
		}
		
		private function display():void
		{
			controlWidth = getTextWidth();
			
			buildBase();
			buildOver();
			buildPress();
			addListeners();
		}
		
		private function getTextWidth():uint
		{
			var itemtxt:TextField = buildText({text:data.label});
			return itemtxt.textWidth + gap;
		}
		
		private function build(item:Sprite, textColor:uint = 0x1f416c):void
		{
			setListItemBase(item, textColor);
		}
		
		private function setListItemBase(item:Sprite, textColor:uint = 0x1f416c):void
		{
			item.graphics.beginFill(0xffffff,0);
			item.graphics.drawRect(0, 0, controlWidth, controlHeight);
			item.graphics.endFill();
			
			var itemtxt:TextField = buildText( {
				width : controlWidth,
				text : data.label,
				color : textColor
			} );
			itemtxt.name = TXT_CHILD_NAME;
			item.addChild(itemtxt);
			itemtxt.x = controlWidth/2 - itemtxt.width/2;
			itemtxt.y = controlHeight/2 - itemtxt.height/2;
			item.alpha = 0;
		}
		
		private function setRootBase(item:Sprite, colors:Array, textColor:uint = 0x1f416c):void
		{
			var alphas:Array = [ 1, 1, 1 ];
			var ratios:Array = [ 0x00, 0xF5, 0xFF ];
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(controlWidth, controlHeight, (Math.PI / 2), 0, 0);
			item.graphics.lineStyle(0, 0xDDDDDD);
			item.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD);
			item.graphics.drawRoundRect(0, 0, controlWidth, controlHeight, rounded, rounded);
			item.graphics.endFill();
			
			var itemtxt:TextField = buildText( {
				width : controlWidth,
				text : data.label,
				color : textColor
			} );
			itemtxt.name = TXT_CHILD_NAME;
			item.addChild(itemtxt);
			itemtxt.x = controlWidth/2 - itemtxt.width/2;
			itemtxt.y = controlHeight/2 - itemtxt.height/2;
			item.alpha = 0;
		}
		
		private function buildText(config:Object) : TextField
		{
			var text:String = (config.text != undefined)?config.text:"";
			var width:uint = (config.width != undefined)?config.width:120;
			var color:uint = (config.color != undefined)?config.color:0xffffff;
			var format:TextFormat = new TextFormat();
			format.font = AppGlobal.FONT_NAME;
			format.color = color;
			format.size = FONT_SIZE;
			format.align = "center";
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.defaultTextFormat = format;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.text = text;
			return txt;
		}
		
		private function addListeners():void
		{
			press.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.addEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function removeListeners():void
		{
			press.removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.removeEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function onRollOver(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,1,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRollOut(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,2,{ease:Strong.easeOut,alpha:0});
			press.alpha = 0;
		}
		
		private function onPress(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,0.5,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRelease(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,1,{ease:Strong.easeOut,alpha:0});
			onClick.dispatch(data);
		}
		
		private function buildBase():void
		{
			build(base, 0x000000);
			base.alpha = 1;
		}
		
		private function buildOver() : void
		{
			build(over, 0x999999);
		}
		
		private function buildPress() : void
		{
			build(press, AppGlobal.RED_ICBC);
		}
		
		public function dispose():void
		{
			removeListeners();
		}
	}
}